<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个人中心</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!-- 引入axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <!-- 引入Vue -->
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
    <!-- import Element UI -->
    <script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
    <link rel="stylesheet" href="css/Myself.css">
</head>
<body>
<div id="app">
    <el-container>
        <el-header height="150px">
            <div style="width: 1400px;margin: 0 auto">
                <img src="../imgs/logo.png" width="300px">
                <a href="">首页</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">热点资讯</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">商家入驻</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">社会招聘</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">校园招聘</a>
                <el-divider direction="vertical"></el-divider>
                <a href="">帮助</a>
                <el-divider direction="vertical"></el-divider>
                <span style="float: right;font-size: 15px;margin-top: 35px">
                    欢迎回来<a href="JavaScript:void(0)" style="text-decoration: none">{{changePerson.nickname}}</a>
                    &nbsp&nbsp&nbsp
                    <a href="JavaScript:void(0)" style="text-decoration: none" @click="logout">退出登录</a>
                </span>
            </div>
        </el-header>
        <el-container>
            <el-aside width="200px">
                <el-menu
                        default-active="4"
                        class="el-menu-vertical-demo"
                        :unique-opened="true"
                        @Select="handleSelect"
                        @open="handleOpen"
                        @close="handleClose">
                    <el-menu-item index="1">
                        <i class="el-icon-s-home"></i>
                        <span slot="title">我的</span>
                    </el-menu-item>
                    <el-menu-item index="2">
                        <i class="el-icon-s-order"></i>
                        <span slot="title">我的订单</span>
                    </el-menu-item>
                    <el-menu-item index="3">
                        <i class="el-icon-shopping-cart-full"></i>
                        <span slot="title">我的购物车</span>
                    </el-menu-item>
                    <el-menu-item index="4">
                        <i class="el-icon-setting"></i>
                        <span slot="title">修改密码</span>
                    </el-menu-item>
                    <el-submenu index="5">
                        <template slot="title">
                            <i class="el-icon-map-location"></i>
                            <span>地址管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="5-1">新增地址</el-menu-item>
                            <el-menu-item index="5-2">地址列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                </el-menu>
            </el-aside>
            <el-main>
                <el-card style="width: 600px;height: 400px;
                margin: 0 auto;background-color: rgba(255, 255, 255, 0.3); text-align: center">
                    <h1 style="font-family: 楷体">修改个人信息</h1>
                    <el-form :model="changePerson" :rules="rules" ref="change" label-width="80px">
                        <el-form-item label="旧密码" prop="oldPassword">
                            <el-input v-model="changePerson.oldPassword" prefix-icon="el-icon-lock" show-password></el-input>
                        </el-form-item>
                        <el-form-item label="新密码" prop="newPassword">
                            <el-input v-model="changePerson.newPassword" prefix-icon="el-icon-lock" show-password></el-input>
                        </el-form-item>
                        <el-form-item label="确认密码" prop="confirmPassword">
                            <el-input v-model="changePerson.confirmPassword" prefix-icon="el-icon-lock"
                                      show-password></el-input>
                        </el-form-item>
                        <el-form-item label="昵称">
                            <el-input v-model="changePerson.nickname"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="change" style="margin-right: 40px">提交</el-button>
                            <el-button type="primary" @click="Reset">重置</el-button>
                        </el-form-item>
                    </el-form>
                </el-card>
            </el-main>
        </el-container>
    </el-container>
</div>
</body>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                changePerson: {
                    id: localStorage.getItem('userId'),
                    oldPassword: '',
                    newPassword: '',
                    confirmPassword: '',
                    nickname: localStorage.getItem('nickname')
                },
                rules: {
                    oldPassword: [
                        {required: true, message: '请输入旧密码', trigger: 'blur'},
                        {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
                    ],
                    newPassword: [
                        {required: true, message: '请输入新密码', trigger: 'blur'},
                        {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
                    ],
                    confirmPassword: [
                        {required: true, message: '请输入确认密码', trigger: 'blur'},
                        {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'},
                        // 对两次的密码进行校验,判断是否相同
                        {
                            validator: (rule, value, callback) => {
                                if (value !== v.changePerson.newPassword) {
                                    callback(new Error('两次密码不一致'))
                                }
                            }
                        }
                    ],
                }
            }
        },
        methods: {
            handleOpen(key, keyPath) {
                console.log("handleOpen = " + key, keyPath);
            },
            handleClose(key, keyPath) {
                console.log("handleClose = " + key, keyPath);
            },
            handleSelect(key, keyPath) {
                if (key === '1') {
                    location.href = 'Myself.html'
                } else if (key === '2') {
                    location.href = 'Order.html'
                } else if (key === '3') {
                    location.href = 'ShoppingCart.html'
                } else if (key === '5-1') {
                    location.href = 'addAddress.html'
                } else if (key === '5-2') {
                    location.href = 'AddressList.html'
                }
            },
            logout() {
                location.href = '../login.html'
            },
            Reset() {
                v.change.oldPassword = '';
                v.change.newPassword = '';
                v.change.confirmPassword = '';
            },
            change() {
                axios.post('/user/change', v.changePerson).then(function (response) {
                    let resonseBody = response.data;
                    if (resonseBody.state === 20000) {
                        v.$message.success('修改成功');
                        location.href = 'Myself.html'
                    } else if (resonseBody.state === 40000) {
                        v.$message.error(resonseBody.message);
                    }
                })
            }
        }
    })
</script>
</html>